//
//  WYwecomeControllerViewController.swift
//  weibo
//
//  Created by 王玉东 on 16/4/10.
//  Copyright © 2016年 wyd. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WYwecomeControllerViewController: UIViewController {
    
    override func loadView() {
        view = UIImageView(image: UIImage(named: "ad_background"))
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

        // Do any additional setup after loading the view.
    }
    //界面数据加载
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-180)
            make.height.width.equalTo(90)//设置图片的宽度
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //设置头像
        
        iconView.cornerRadius = 45
        
        iconView.sd_setImageWithURL(WYUserAccaseViewModel.sharedViewModel.herdUrl)
          //
        
    }
    private func startAnimation() {
        let offset = -ScreenHeight + 180
        nameLabel.alpha = 0
        //执行动画  弹簧动画
      
        self.iconView.snp_updateConstraints(closure: { (make) -> Void in
            //更新 头像的地步约束
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        })
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            //强制刷新页面 提前刷新页面
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
            UIView.animateWithDuration(0.6, animations: { () -> Void in
                self.nameLabel.alpha = 1
                }, completion: { (_) -> Void in
                   //跳转界面
                    NSNotificationCenter.defaultCenter().postNotificationName(WYSwitchRootViewController, object: "main")
            })
                
               // print("👌")
        }
    }
    
    //懒加载控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
//    private lazy var nameLabel: UILabel = {
//        let l = UILabel()
//        l.textColor = UIColor.darkGrayColor()
//        l.font = UIFont.systemFontOfSize(15)
//        l.text = WYUserAccaseViewModel.sharedViewModel.userAccout?.name
//        //设置文字
//        print(l.text)
//        //l.text = .sharedAccountViewModel.userAccount?.name
//        l.sizeToFit()
//        return l
//    }()
    private lazy var nameLabel:UILabel = UILabel(text: WYUserAccaseViewModel.sharedViewModel.userAccout?.name ?? "", fontSize: 16.0, textColor: UIColor.darkGrayColor())
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
